/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.control;

import it.polimi.meteocal.entity.Event;
import it.polimi.meteocal.entity.InvitedList;
import it.polimi.meteocal.entity.InvitedListPK;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author paolo
 */
@Stateless
public class InvitationManager {

    @PersistenceContext
    private EntityManager em;
    @EJB
    private UserManager um;

    /**
     * store in the database all the user invited to the event
     *
     * @param event
     * @param invitedUsers
     */
    public void addInvitation(Event event, List<String> invitedUsers) {
        for (String invitedUser : invitedUsers) {
            InvitedListPK invitedListPK = new InvitedListPK();
            invitedListPK.setEvent(event.getEventID());
            InvitedList invitedList = new InvitedList();
            invitedListPK.setUser(invitedUser);
            invitedList.setInvitedListPK(invitedListPK);
            em.persist(invitedList);
            em.flush();
        }
    }

    /**
     * set the invitation as accepted
     *
     * @param event
     */
    public void accept(Event event) {
        InvitedListPK pk = new InvitedListPK();
        pk.setEvent(event.getEventID());
        pk.setUser(um.getLoggedUser().getEmail());
        InvitedList invitedList = em.find(InvitedList.class, pk);
        invitedList.setPartecipation(Boolean.TRUE);
        em.merge(invitedList);
        em.flush();
    }

    /**
     * set the invitation as declined
     *
     * @param event
     */
    public void decline(Event event) {
        InvitedListPK pk = new InvitedListPK();
        pk.setEvent(event.getEventID());
        pk.setUser(um.getLoggedUser().getEmail());
        InvitedList invitedList = em.find(InvitedList.class, pk);
        invitedList.setPartecipation(Boolean.FALSE);
        em.merge(invitedList);
        em.flush();
    }

    /**
     *
     * @param event
     * @return true if the logged user as alreay answered to the invitation,
     * false otherwise
     */
    public boolean alreadyAnswered(Event event) {
        InvitedListPK pk = new InvitedListPK();
        pk.setEvent(event.getEventID());
        pk.setUser(um.getLoggedUser().getEmail());
        InvitedList invitedList = em.find(InvitedList.class, pk);
        if (invitedList.getPartecipation() == null) {
            return Boolean.FALSE;
        } else {
            return Boolean.TRUE;
        }
    }

    /**
     *
     * @param event
     * @param user
     * @return true if the user partecipate to the event, false otherwise
     */
    public boolean partecipate(Event event, String user) {
        InvitedListPK pk = new InvitedListPK();
        pk.setEvent(event.getEventID());
        pk.setUser(user);
        InvitedList invitedList = em.find(InvitedList.class, pk);
        if (invitedList.getPartecipation() != null) {
            if (invitedList.getPartecipation()) {
                return Boolean.TRUE;
            } else {
                return Boolean.FALSE;
            }
        } else {
            return Boolean.FALSE;
        }
    }

    /**
     *
     * @param event
     * @param user
     * @return true if th user is invited to the event, false otherwise
     */
    public boolean invited(Event event, String user) {
        InvitedListPK pk = new InvitedListPK();
        pk.setEvent(event.getEventID());
        pk.setUser(user);
        InvitedList invitedList = em.find(InvitedList.class, pk);
        if (invitedList != null) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }
}
